home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 636 < prev    next >
Encoding:
Text File  |  1996-08-06  |  6.2 KB  |  208 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Some ideas about C++ and a question
  5. Date: 05 Mar 1996 09:47:05 PST
  6. Organization: GABI Software, Sarl.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <KANZE.96Mar5124636@slsvgqt.lts.sel.alcatel.de>
  9. References: <4h9se5$m2n@s3.iway.fr>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 05 Mar 1996 11:46:36 GMT
  12. In-Reply-To: Valentin Bonnard's message of 04 Mar 1996 10:21:57 PST
  13. Apparently-To: std-c++@ncar.UCAR.EDU
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMTx+NEy4NqrwXLNJAQGBQgIAqBUpI9x3311/jCHRuZDwjTzPDaAT73lf
  16.     mYkQSF3slRMQxlP5Ni6bdTo0QETWlz1XCbExsXxDjjZPmhhcryGxiA==
  17.     =DY7c
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. In article <4h9se5$m2n@s3.iway.fr> Valentin Bonnard <bonnardv@pratique.fr> writes:
  21.  
  22. |> I have 4 ideas about C++ and a question.
  23.  
  24. |> 1) String literals
  25. |> ~~~~~~~~~~~~~~~~~~
  26. |> String literals should be const char* instead of char* (if it is not
  27. |> already the case).
  28.  
  29. I agree.  There was some discussion about this in the committee, but
  30. as far as I know, there was never a real proposal.
  31.  
  32. |> 2) Placement operators
  33. |> ~~~~~~~~~~~~~~~~~~~~~~
  34. |> class Object {
  35. |>         void    operator+ // or / or whatever
  36. |>                          (Object& result, const Object& b) const;
  37. |>         void    operator* (Object& result) const;
  38. |>         Object  operator* (const Object& result) const; // old one
  39. |> }
  40.  
  41. |> should be called respectively by:
  42. |> a = b + c;
  43.  
  44. |> and a = *b;
  45.  
  46. |> and (old one) a*b;
  47.  
  48. |> This will solve the [well-known] problem of copying the result into a temporary.
  49. |> The compatibility will be preserved since old syntax will be allowed to.
  50.  
  51. Presented like this, the idea sounds good.  Now try and define it
  52. exactly.  In which cases is which operator used, and how?  What are
  53. the exact semantics?  I think you'll find that it introduces an
  54. enormous amount of additional confusion in an area which is already
  55. not particularly clear.
  56.  
  57. |> 3) Use of explicit keyword
  58. |> ~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. |> //========= Test.h =========
  60. |> class Base {
  61. |>     public:
  62. |>         virtual void VirtualFunc ();
  63. |> };
  64.  
  65. |> class Derived : public Base {
  66. |>     public:
  67. |>         void VirtualFunc () { }
  68. |> };
  69.  
  70. |> //========= Caller.cp =========
  71. |> #include <Test.h>
  72. |> class Derived2 : public Derived {
  73. |>     public:
  74. |>         void VirtualFunc ();
  75. |> };
  76.  
  77. |> //========= Foo.cp =========
  78. |> #include <Test.h>
  79.  
  80. |> void    foo (explicit Derived* object)
  81. |> {
  82. |>     object.VirtualFunc (); // nop (compiler does not generate code for this)
  83.  
  84. |>     Derived* AnyDerivedObject = object; // ok
  85. |> }
  86.  
  87. |> void    foo2 (Derived* object)
  88. |> {
  89. |>     object.VirtualFunc (); // object can be Derived2
  90.  
  91. |>     explicit Derived* RealDerivedObject = object; // error
  92. |> }
  93.  
  94. |> The meaning of explicit could be: while a derived class ptr [resp. ref] can be
  95. |> convert to a base class ptr [ref], it can't be converted to a base class 
  96. |> explicit ptr [ref].
  97.  
  98. |> So explicit would be a type qualifier; this usage is different with:
  99.  
  100. Seems confusing, and I really cannot see any use for it.  If the
  101. derived class cannot be used where ever the base class is used, you
  102. shouldn't be using (public) derivation.
  103.  
  104. |> > Rich Hickey
  105. |> > rich@rcs-hq.mhs.compuserve.com
  106.  
  107. |> > I propose that the explicit keyword be allowed as a qualifier of a class
  108. |> > definition, such qualification taking the form of:
  109.  
  110. |> > explicit class X{
  111. |> >   //the definition of X
  112. |> >   };
  113.  
  114. |> Here, X cannot be derived but my Derived can be so I think my idea is better
  115. |> (please fell free comment/criticise this assertion).
  116.  
  117.  
  118. |> 4) Conversion of Type** to const Type**
  119. |> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120.  
  121. |> It's ok, this is an error; but why cannot Type** be converted to const Type* 
  122. |> const* ?
  123.  
  124. |> void    foo ()
  125. |> {
  126. |>     typedef int Type;
  127. |>     Type        i;
  128. |>     const Type  ci = 2;
  129. |>     Type*       pi = &i;
  130. |>     Type**      ppi = π
  131.  
  132. |>     const Type** ppci = ppi;            // error because
  133. |>     *ppci = &ci;                        // now pi == &ci
  134. |>     *pi = 0;                            // and now we modify ci
  135.  
  136. |>     const Type*const* pcpci = ppi;      // no problem because
  137. |>     *pcpci = &ci;                       // this is impossible (since *pcpci is 
  138. |> const)
  139. |> }
  140.  
  141.     char const    c( 'a' ) ;
  142.     char const*    pc( &c ) ;
  143.     char*        p ;
  144.     char const**    ppc( &p ) ;    //  Currently illegal, since
  145.                     //  &p has type char**, not
  146.                     //  char const**.
  147.     *ppc = pc ;
  148.     *p = 'b' ;            //  Guess what is accessed.
  149.  
  150. Const is violated *without* ever being cast away.
  151.  
  152. |> Now a question:
  153.  
  154. |> Will there be any way to redefine (overload) normal && or || on class ?
  155. |> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  156.  
  157. |> I mean, now:
  158.  
  159. |> class Int {
  160. |>     public:
  161. |>         operator int () const { return i; }
  162. |>         operator && (int) const;
  163. |>         operator || (int) const;
  164. |>         Int (int I) : i(I) { }
  165.  
  166. |>     private:
  167. |>         int     i;
  168. |> };
  169.  
  170. |> int     Func ()
  171. |> {
  172. |>     cout << "called\n";
  173. |>     return 1;
  174. |> }
  175.  
  176. |> void    foo ()
  177. |> {
  178. |>     int i (1);
  179. |>     Int I (1);
  180.  
  181. |>     if (i || Func ()) // called is not printed
  182. |>         ;
  183. |>     if (I || Func ()) // called is printed
  184. |>         ;
  185. |> }
  186.  
  187. |> One could think that the standard should be change in order to support real
  188. |> overloaded operator && and ||.
  189.  
  190. How?  Write a definition of how you think overloaded operator&& and ||
  191. should work.
  192.  
  193. In the above case, IMHO, the correct solution is not to overload
  194. operator||, but to define a conversion function for Int, e.g.:
  195. Int::operator bool(), for example.
  196. -- 
  197. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  198. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  199. Conseils, itudes et rialisations en logiciel orienti objet --
  200.                 -- A la recherche d'une activiti dans une region francophone
  201. ---
  202. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  203.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  204.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  205.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  206.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  207. ]
  208.